package com.matt.sooncleaner

import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.ToastUtils
import com.cloud.cleanjunksdk.tools.Utils
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.matt.sooncleaner.arch.ArchActivity
import com.matt.sooncleaner.databinding.ActivityMainBinding
import com.matt.sooncleaner.junk.CleanManager
import com.matt.sooncleaner.junk.JunkActivity
import com.matt.sooncleaner.menu.MenuActivity
import com.matt.sooncleaner.notification.NotificationHelper
import com.matt.sooncleaner.notification.NotificationManagerActivity
import com.matt.sooncleaner.wifiscan.WifiDisconnectTipDialog
import com.matt.sooncleaner.wifiscan.WifiScanActivity
import com.matt.sooncleaner.wifiscan.getLastWifiScanTime


class MainActivity : ArchActivity<ActivityMainBinding>() {
    private companion object {
        private val WIFI_SCAN_PERMISSION = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION
        )
    }

    private val mWifiScanPermissionsLauncher =
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
            if (it.isAllGranted()) {
                startActivity(Intent(this@MainActivity, WifiScanActivity::class.java))
            } else {
                ToastUtils.showLong(R.string.permission_agree_tip)
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.toolbar.setNavigationOnClickListener {
            startActivity(Intent(this, MenuActivity::class.java))
        }
        binding.wifiScan.setOnClickListener {
            if (!NetworkUtils.isWifiConnected()) {
                WifiDisconnectTipDialog(this).show()
            } else {
                mWifiScanPermissionsLauncher.launch(WIFI_SCAN_PERMISSION)
            }
        }
        binding.cleanCard.setOnClickListener {
            val isGranted = CleanManager.requestPermission(this)
            if (isGranted) {
                gogoCleaJunk()
            }
        }
        binding.notificationManageCard.setOnClickListener {
            if (!NotificationHelper.isNotificationListenerEnable()) {
                MaterialAlertDialogBuilder(this)
                    .setTitle(R.string.notification_permission_allow)
                    .setMessage(R.string.notification_permission_content)
                    .setPositiveButton(
                        R.string.allow
                    ) { dialog, which -> NotificationHelper.gotoNotificationAccessSetting(this@MainActivity) }
                    .show()
            } else {
                startActivity(Intent(this, NotificationManagerActivity::class.java))
            }
//            val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        binding.lastScanTime.text = getLastWifiScanTime()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Utils.ACCESS_ALL_FILES_REQUEST) {
            val getPermission = Utils.requestForPermission(this, this)
            if (getPermission) {
                gogoCleaJunk()
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            Utils.EXTERNAL_REQUEST -> {
                if (grantResults.isNotEmpty()
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED
                ) {
                    gogoCleaJunk()
                } else {
                    ToastUtils.showLong(R.string.need_read_storage_permission)
                }
            }
        }
    }

    private fun gogoCleaJunk() {
        if (CleanManager.canUseCleanJunk()) {
            startActivity(Intent(this, JunkActivity::class.java))
        }
    }

    private fun Map<String, Boolean>.isAllGranted() =
        count { it.value } == size
}